<template>
    <div class="header">
        <div class="nav-topbar">
            <div class="container">
                <div class="topbar-menu">
                    <a href="javascript:;">传奇商城</a>
                    <a href="javascript:;">手机</a>
                    <a href="javascript:;">电脑</a>
                    <a href="javascript:;">笔记本</a>
                </div>
                <div class="topbar-user">
                    <a href="javascript:;" v-if="username">{{username}}</a>
                    <a href="javascript:;" v-if="!username" @click="login">登录</a>
                    <a href="javascript:;" v-if="username" @click="logout">退出</a>
                    <a href="/#/order/list" v-if="username">我的订单</a>
                    <a href="javascript:;" class="my-cart" @click="goToCart"><span class="icon-cart"></span>购物车({{cartCount?cartCount:0}})</a>
                </div>
            </div>
        </div>
        <div class="nav-header">
            <div class="container">
                <div class="header-logo"><a href="/#/index"></a></div>
                <div class="header-menu">
                    <div class="item-menu">
                        <span>最新手机</span>
                        <div class="children">
                            <ul>
                                <li class="product" v-for="(item,index) in phoneList" :key="index">
                                    <a :href="'/#/product/'+item.id" target="_blank">
                                        <div class="pro-img">
                                            <img v-lazy="item.mainImage" :alt="item.subtitle">
                                        </div>
                                        <div class="pro-name">{{item.name}}</div>
                                        <div class="pro-price">{{item.price | currency}}</div>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="item-menu">
                        <span>热卖手机</span>
                        <div class="children"></div>
                    </div>
                    <div class="item-menu">
                        <span>经典手机</span>
                        <div class="children"></div>
                    </div>
                </div>
                <div class="header-search">
                    <div class="wrapper">
                        <input type="text" name="keyword">
                        <a href="javascript:;"></a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import {mapState} from 'vuex';

    export default {
        name: "nav-header",
        data() {
            return {
                phoneList: []
            }
        },
        // 用计算属性解决数据延时问题
        computed: {
            /*          username() {
                          return this.$store.state.username;
                      },
                      cartCount() {
                          return this.$store.state.cartCount;
                      }*/
            // 简化写法
            ...mapState(['username', 'cartCount'])
        },
        mounted() {
            this.getProductList();
            let params = this.$route.params;
            if(params && params.from === 'login'){
                this.getCartCount();
            }
        },
        filters: {
            // 过滤器:转换时间、转换金钱
            currency(val) {
                if (!val) return '0.00';
                return '￥' + val.toFixed(2) + '元'; // 保留2位小数
            }
        },
        methods: {
            login() {
                this.$router.push('/login');
            },
            getProductList() {
                this.axios.get('/products', {
                    params: {
                        categoryId: '100012',
                        pageSize: 6
                    }
                }).then((res) => {
                    this.phoneList = res.list;
                })
            },
            goToCart() {
                this.$router.push('/cart');
            },
            getCartCount() {
                this.axios.get('/carts/products/sum').then((res)=>{
                    // 分发Action
                    this.$store.dispatch('saveCartCount',res);
                })
            },
            logout() {
                this.axios.post('/user/logout').then(() => {
                    this.$message.success('退出成功');
                    this.$cookie.set('userId','',{expires:'-1'});
                    this.$store.dispatch('saveUserName','');
                    this.$store.dispatch('saveCartCount','0');
                })
            }
        }
    }
</script>

<style scoped lang="scss">
    @import "../assets/scss/base";
    @import "../assets/scss/config";
    @import "../assets/scss/mixin";

    .header {
        .nav-topbar {
            height: 39px;
            line-height: 39px;
            background-color: #333333;
            color: #B0B0B0;
            .container {
                @include flex();
                a {
                    display: inline-block;
                    color: #B0B0B0;
                    margin-right: 17px;
                }
                .my-cart {
                    width: 110px;
                    background-color: #FF6600;
                    text-align: center;
                    color: #ffffff;
                    margin-right: 0;
                    .icon-cart {
                        @include bgImg(16px, 12px, '/imgs/icon-cart-checked.png');
                        margin-right: 4px;
                    }
                }
            }
        }
        .nav-header {
            .container {
                position: relative;
                height: 112px;
                @include flex();
                .header-menu {
                    display: inline-block;
                    width: 643px;
                    padding-left: 209px;
                    .item-menu {
                        display: inline-block;
                        color: #333333;
                        font-weight: bold;
                        font-size: 16px;
                        line-height: 112px;
                        margin-right: 20px;
                        span {
                            cursor: pointer; // 鼠标手势
                        }
                        &:hover {
                            color: $colorA;
                            .children { // 鼠标移到span显示children
                                height: 220px;
                                opacity: 1;
                            }
                        }
                        .children {
                            position: absolute;
                            top: 112px;
                            left: 0;
                            width: 1226px;
                            height: 0;
                            opacity: 0;
                            overflow: hidden;
                            border-top: 1px solid #e5e5e5;
                            box-shadow: 0 7px 6px 0 rgba(0, 0, 0, 0.11);
                            z-index: 10;
                            transition: all .3s;
                            background-color: $colorG;
                            .product {
                                position: relative; // 相对定位
                                float: left;
                                width: 16.6%;
                                height: 220px;
                                font-size: $fontK;
                                line-height: 12px;
                                text-align: center;
                                a {
                                    display: inline-block;
                                }
                                img {
                                    height: 111px;
                                    width: auto;
                                    margin-top: 26px;
                                }
                                .pro-img {
                                    height: 137px;
                                }
                                .pro-name {
                                    font-weight: bold;
                                    margin-top: 19px;
                                    margin-bottom: 8px;
                                    color: $colorB;
                                }
                                .pro-price {
                                    color: $colorA;
                                }
                                &:before { // 右边加一条竖线
                                    content: '';
                                    position: absolute; // 子元素绝对定位,父元素要开启相对定位
                                    top: 28px;
                                    right: 0;
                                    border-right: 1px solid $colorF;
                                    height: 100px;
                                }
                                &:last-child:before { // 隐藏最右边的竖线
                                    display: none;
                                }
                            }
                        }
                    }
                }
                .header-search {
                    width: 319px;
                    .wrapper {
                        height: 50px;
                        border: 1px solid #E0E0E0;
                        display: flex;
                        align-items: center;
                        input {
                            border: none;
                            border-right: 1px solid #E0E0E0;
                            width: 264px;
                            height: 50px;
                            padding-left: 14px;
                            box-sizing: border-box; // 盒子模型 宽度不包括padding-left
                        }
                        a {
                            @include bgImg(18px, 18px, '/imgs/icon-search.png');
                            margin: auto;
                        }
                    }
                }
            }
        }
    }
</style>